package examination;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SetInvigilatorServlet
 * @author ChangingSelf
 */
@WebServlet("/SetInvigilatorServlet")
public class SetInvigilatorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SetInvigilatorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * 设置监考老师。接收考试编号和职工号参数，调用InvigilatorBean的方法将此老师添加为监考老师。
		 * 并跳转回当前登录老师的“我的监考”页面（即将他的职工号作为GET参数跳转到监考详情表）
		 */
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String examNo = request.getParameter("examNo");
		String staffNo = request.getParameter("staffNo");
		String isMainString = request.getParameter("isMain");
		
		String successUrl = "invigilation_detail.jsp";
		String failUrl = "exam_info.jsp";
		
		//如果参数不全，则跳转回上一页
		if(examNo==null||staffNo==null||isMainString==null) {
			response.sendRedirect(request.getHeader("Referer"));
			return;
		}
		
		
		boolean isMain = "true".equals(isMainString); 
		
		boolean success = InvigilatorBean.arrange(examNo, staffNo, isMain);
		
		if(success) {
			response.getWriter().write("安排成功！3秒钟跳转");
			//设置3秒钟跳转
			response.setHeader("refresh", "3;url="+successUrl+"?staffNo="+staffNo);
			return;
		}else {
			
			response.getWriter().write("修改失败！3秒钟跳转");
			//设置3秒钟跳转
			response.setHeader("refresh", "3;url="+failUrl);
			return;
		}
		
		
	}

}
